
if (SERVER) then
 
 
   AddCSLuaFile ("shared.lua");
   SWEP.Weight = 5;
   SWEP.AutoSwitchTo = false;
   SWEP.AutoSwitchFrom = false;
 
end
 
if (CLIENT) then

	SWEP.Author				= "Chopknee"
	SWEP.Purpose			= "Used for the garry's mod dodgeball gamemode."
	SWEP.Instructions		= "Primary: Throw far\nSecondary: Throw short"
	SWEP.Slot				= 3
	SWEP.SlotPos			= 2
	SWEP.PrintName = "Throwing Arm";
	SWEP.Slot = 0;
	SWEP.SlotPos = 0;
	SWEP.DrawAmmo = false;
	SWEP.DrawCrosshair = true;
 
end
 
SWEP.Spawnable			= true
SWEP.AdminSpawnable		= false

SWEP.ViewModel			= "models/weapons/v_throwingarm.mdl"
SWEP.WorldModel			= "models/weapons/w_grenade.mdl"
SWEP.HoldType			= "grenade"

SWEP.Primary.Sound			= Sound("weapons/slam/throw.wav")
SWEP.Primary.Recoil			= 0
SWEP.Primary.Unrecoil		= 0
SWEP.Primary.Damage			= 0
SWEP.Primary.NumShots		= 1
SWEP.Primary.Cone			= 0
SWEP.Primary.Delay			= 1


SWEP.Primary.ClipSize		= -1
SWEP.Primary.DefaultClip	= 0
SWEP.Primary.Automatic		= false
SWEP.Primary.Ammo			= "grenade"

SWEP.Secondary.ClipSize		= -1
SWEP.Secondary.DefaultClip	= -1
SWEP.Secondary.Automatic	= false
SWEP.Secondary.Ammo			= "none"
SWEP.Secondary.Delay		= 10

--No reloading
function SWEP:Reload()
	--self.Owner:DrawViewModel(true)
	--self.Owner:DrawWorldModel(true)

	--self.NextDeploy = CurTime() + 0.025

	--GAMEMODE:WeaponDeployed(self.Owner, self)

	return true
end

--Set the swep to idle
function SWEP:Deploy()
	self.Weapon:SendWeaponAnim( ACT_VM_IDLE )
	return true
end

function SWEP:Think()
	if (self.NextDeploy and self.NextDeploy < CurTime()) then
		if (self.Owner:GetAmmoCount(self.Primary.Ammo) > 0) then
			self.Weapon:SendWeaponAnim( ACT_VM_IDLE )
		else
		
		end
	end
	if (SERVER) then
		if (string.find(self.Owner:Nick(), "Bot") == nil and self.Owner:SteamID() != "BOT") then
			if (self.Owner:GetAmmoCount(self.Primary.Ammo) > self.Owner.MaxBallCount) then
				local owner = self.Owner
				local ent = ents.Create("ent_ball_disabled")
				if ent:IsValid() then
					--self.Owner:ChatPrint("You can't hold more than " .. self.Owner.MaxBallCount .. " balls.")
					ent:SetPos(owner:GetShootPos())
					ent:SetOwner(owner)
					ent:Spawn()
					self:TakePrimaryAmmo(1)
				end
			end
		end
	end
end

--Not really necessarry
function SWEP:Holster()
	return true
end

function SWEP:CanPrimaryAttack()
	if (self.Owner:GetAmmoCount(self.Primary.Ammo) > 0) then
		return true
	else
		self:SetNextPrimaryFire(CurTime() + self.Primary.Delay)
		return false
	end
end

function SWEP:throw_attack (throw_power)

	if not self:CanPrimaryAttack() then 
	return end
	self:SetNextPrimaryFire(CurTime() + self.Primary.Delay)
	self:TakePrimaryAmmo(1)

	local tr = self.Owner:GetEyeTrace();
 

	self.Weapon:EmitSound (self.Primary.Sound);
	self.BaseClass.ShootEffects (self);

	self.NextDeploy = CurTime() + 1
	
	if (!SERVER) then return end;
 
	--Firing the ball
	local owner = self.Owner
	local ent = ents.Create("ent_ball")
	if ent:IsValid() then
		ent:SetPos(owner:GetShootPos())
		ent:SetOwner(owner)
		ent:Spawn()
		ent:EmitSound(self.Primary.Sound)
		local phys = ent:GetPhysicsObject()
		if phys:IsValid() then
			phys:Wake()
			phys:AddAngleVelocity(VectorRand():Angle() * 3)
			phys:SetVelocityInstantaneous(owner:GetAimVector() * throw_power)
		end
	end
	ent:SetOwner(owner)
	--This is not nessessarry for the gamemode GMDodgeball, but it is for sandbox in case someone decides to use it in sandbox
	--cleanup.Add (self.Owner, "entity", ent);
	--undo.Create ("Thrown ball");
	--undo.AddEntity (ent);
	--undo.SetPlayer (self.Owner);
	--undo.Finish();
end
 
--far throw
function SWEP:PrimaryAttack()
		self:throw_attack (75000);
		self.Weapon:SendWeaponAnim( ACT_VM_THROW )
end

--short throw
function SWEP:SecondaryAttack()
		self:throw_attack (3000);
		self:SetNextSecondaryFire(CurTime() + self.Primary.Delay)
		self.Weapon:SendWeaponAnim( ACT_VM_SECONDARYATTACK )
end